home *** CD-ROM | disk | FTP | other *** search
- Path: news.mira.net.au!news
- From: davidw@werple.net.au (David White)
- Newsgroups: comp.lang.c++
- Subject: Re: How big is the 0
- Date: 19 Feb 1996 20:05:10 +1100
- Organization: Werple Internet, Melbourne
- Message-ID: <4g9eg6$pks@werple.net.au>
- References: <1996Feb17.132420.114207@kuhub.cc.ukans.edu>
- NNTP-Posting-Host: werple.mira.net.au
-
- anh@kuhub.cc.ukans.edu writes:
-
-
- >Hello,
-
- >In C, anytime I need to do a null pointer check I would do this:
-
- > MyType *p;
-
- > if(p==(MyType*)0)
- > bla;
-
- >The new operator supposedly return a 0 upon failure. Can I rely on the
- >compiler to make sure the 0 is as big as the size of the pointer? For
- >example, ALPHA's pointers are 64 bits long which is different from the 32 bits
- >int.
-
- The 0 will be the same size as the pointer size. This 0 has nothing to do
- with the number 0. It isn't even guaranteed to contain all zero bits. A
- compiler, if it wishes, can use different bit patterns for different
- types, e.g., (char*)0 and (int*)0 can't be guaranteed to contain the same
- bit pattern, but it doesn't matter because the compiler would choose the
- 'right' value for the type anywhere you test or assign a pointer to 0.
- Null pointers are fundamental to the language; very few programs would
- run correctly if the compiler did not implement them correctly.
-
- BTW, I believe, according to the latest draft standard, that 'new' throws
- an exception if it fails, rather than returning 0. Also, you don't need to
- cast the 0; in the code above, 'if(p == 0)' or 'if(!p)' is the usual
- practice.
-
- David White
- davidw@werple.mira.net.au
-